<template>
	<BaseContainer
		class="report-table-wrapper"
		v-loading="loading"
		element-loading-text="加载中..."
		element-loading-background="rgba(255, 255, 255, 0.8)"
	>
		<div class="r-header">
			<p>南京市养老机构新增床位补贴申请表</p>
		</div>
		<ReportTable
			:column="dataListBase"
			:isDetail="isDetail"
			:moreNewsModel="moreNewsModel"
			:baseModel="baseModel"
			:model="modelData"
			@change="handleSave"
		>
		</ReportTable>
		<ReportTable
			:column="dataListStaff"
			:isDetail="isDetail"
			:moreNewsModel="moreNewsModel"
			:baseModel="baseModel"
			:model="modelData"
			@change="handleSave"
		></ReportTable>
		<ReportTable
			:column="dataListApply"
			:isDetail="isDetail"
			:moreNewsModel="moreNewsModel"
			:baseModel="baseModel"
			:model="modelData"
			@change="handleSave"
		>
			<template v-slot:note>
				<div class="w-100 tr flex-column">
					<p class="fc">本机构承诺以上及所附数据资料真实有效，如有不实，愿承担相关处罚。</p>
					<div class="p-r-50">
						<p>负责人签名： （单位盖章）</p>
						<p style="padding-right: 3vw">年 月 日</p>
					</div>
				</div>
			</template>
		</ReportTable>
		<ReportTable :column="dataListFooter" :model="modelData" :baseModel="editData" @change="handleSave">
      <template v-slot:opinionS>
				<div class="w-100 tr flex-column" style="line-height:30px">
					<p style="padding-right: 1vw"> 街（镇）民政部门：（章）</p>
					<p style="padding-right: 3vw">年 月 日</p>
				</div>
			</template>
      <template v-slot:opinionT>
				<div class="w-100 tr flex-column" style="line-height:30px">
					<p style="padding-right: 1vw">第三方机构：（章）</p>
					<p style="padding-right: 3vw">年 月 日</p>
				</div>
			</template>
			<template v-slot:opinion>
				<div class="w-100 tr flex-column" style="line-height:30px">
					<p style="padding-right: 1vw">区民政局：（章）</p>
					<p style="padding-right: 3vw">年 月 日</p>
				</div>
			</template>
		</ReportTable>

		<aplid-dialog
			@close="onClose"
			width="85%"
			v-model="visibleForm"
			append-to-body
			whickComm="Archives"
			:customeHeader="customeHeader"
			:model="nowOrgInfo"
		>
			<transition name="fade" mode="out-in">
				<div class="dialog-content w-100" v-show="!Loading">
					<ServiceOrgArchives :showBtn="false" :nowRowData="nowOrgInfo" :tabsList="tabsList"></ServiceOrgArchives>
				</div>
			</transition>
			<PageLoading v-if="Loading" />
		</aplid-dialog>

		<el-drawer size="65%" append-to-body v-model="drawer" :with-header="false" @close="onClose">
			<practitioner :needPigeonhole="false" v-if="drawer" :nowRowData="model" :isComprehensiveQueries="false" :actionBtn="false" />
		</el-drawer>

		<el-drawer size="65%" append-to-body v-model="drawerOldman" :with-header="false">
			<admissionManagement
				:needPigeonhole="false"
				v-if="drawerOldman"
				:paramsObj="{
					creditCode: creditCode,
				}"
				:menuBtn="[
					{
						label: '查看详情',
						type: 'primary',
						operate: 'detail',
						auth: 'view_dpElderlyCheckinManage_page',
						text: true,
						show: true,
					},
				]"
				:isComprehensiveQueries="false"
				:actionBtn="false"
			/>
		</el-drawer>
	</BaseContainer>
</template>

<script setup lang="ts">
import { onUnmounted } from 'vue'
import ReportTable from '../ReportTable/index.vue'
import apiMod from '@/apiMod'
import ServiceOrgArchives from '@/views/datacenter/basicData/serviceOrg/components/Archives/index.vue'
import practitioner from './practitioner/index.vue'
import admissionManagement from '@/views/elderlyInstitute/admissionManagement/index.vue'
import { useCommonStore } from '@/stores/common'
import { filterEmptyObj } from '@/utils/util'
import { ElMessage, ElMessageBox } from 'element-plus'

const visibleForm = ref(false)
const { setTempParams, setGloablParams, setMoreSearch } = useCommonStore()
const { Loading } = storeToRefs(useCommonStore())

const props = withDefaults(
	defineProps<{
    otherData?:any
		creditCode?: string | number
		params?: any
		editData?: any
		isDetail?: boolean
		caseId?: string | number
		custome?: boolean
		approvalStatus?: string | number
	}>(),
	{
		editData: {},
	}
)

const emit = defineEmits(['save'])
const loading = ref(false)
const model = ref({
	year: '',
	month: '',
})
const baseModel: any = shallowRef({})
let moreNewsModel: any = ref({})
let modelData: any = ref({})
const pageReload: any = inject('reload')

const drawer = ref(false)
const drawerOldman = ref(false)
const nowOrgInfo = ref({})

const tabsList = [
	{
		name: 'BaseDetail',
		label: '基本信息',
	},
	{
		name: 'information',
		label: '资料信息',
	},
]
const customeHeader = {
	type: 'CardOne',
	title: 'organizationName',
	avatar: 'organizationLogo',
	tipList: [{ prop: 'operateStatusLabel', type: 'tip-success', label: '运营状态' }],
	tagList: [
		{ prop: 'organizationTypeLabel', custome: 'tag', type: 'success', label: '机构类型' },
		{ prop: 'integrity', type: 'success', label: '材料完整度' },
	],
	menuBtn: [],
}

const onClose = () => {
	setTempParams({})
	setGloablParams({})
	setMoreSearch({})
}

const getServiceOrgInfo = (creditCode) => {
	if (!creditCode) return
	apiMod.view_dpServiceOrgBase_creditCode(creditCode).then((res) => {
		nowOrgInfo.value = res.data
		visibleForm.value = true
	})
}
const dataListBase = [
	[
		{
			m: '养老机构基本情况',
			bl: 1,
			fs: 16,
			mc: {
				rs: 1,
				cs: 6,
			},
		},
	],
	[
		{ m: '机构名称', style: { width: '200px' } },
		{
			m: '',
			prop: 'organizationName',
			mc: {
				rs: 1,
				cs: 3,
			},
			setColor: true,
			archives: true,
			archivesText: '档案',
			showLink: (col, model) => {
				getServiceOrgInfo(model.creditCode)
			},
		},
		{ m: '法定代表人', style: { width: '200px' } },
		{ m: '', prop: 'legalUserName', setColor: true },
	],
	[
		{ m: '机构地址' },
		{
			m: '',
			prop: 'organizationAddress',
			mc: {
				rs: 1,
				cs: 3,
			},
		},
		{ m: '邮政编码' },
		{ m: '', setColor: true, prop: 'postalCode' },
	],
	[
		{ m: '联系电话' },
		{ m: '', setColor: true, prop: 'legalUserPhone' },
		{
			m: '身份证号码',
			style: {
				width: '160px',
			},
		},
		{
			m: '',
			setColor: true,
			prop: 'legalIdentifyCode',
			mc: {
				rs: 1,
				cs: 3,
			},
		},
	],
	[
		{ m: '占地面积' },
		{ m: '', prop: 'organizationCoverArea', setColor: true },
		{ m: '使用面积' },
		{ setColor: true, m: '', prop: 'organizationFloorSpace' },
		{ m: '投资总额' },
		{ setColor: true, m: '', prop: 'investAmountTotal' },
	],
	[
		{ m: '机构负责人' },
		{ m: '', prop: 'burdenUserName', setColor: true },
		{ m: '联系电话' },
		{ setColor: true, m: '', prop: 'burdenUserPhone' },
		{ m: '区核定床位数' },
		{ setColor: true, m: '', prop: 'approvedBedTotal' },
	],
	[
		{ m: '资金来源' },
		{ m: '', prop: 'fundingSource' },
		{ m: '电子邮件' },
		{
			m: '',
			prop: 'email',
			mc: {
				rs: 1,
				cs: 3,
			},
		},
	],
	[
		{ m: '营业执照（民非）证号' },
		{
			setColor: true,
			m: '',
			prop: 'creditCode',
			mc: {
				rs: 1,
				cs: 2,
			},
		},
		{
			m: '机构设置证号（备案）',
			mc: {
				rs: 1,
				cs: 2,
			},
		},
		{ setColor: true, m: '', prop: 'filingCode' },
	],
	[
		{ m: '注册资金' },
		{
			setColor: true,
			m: '',
			prop: 'registeredCapital',
			mc: {
				rs: 1,
				cs: 2,
			},
		},
		{
			m: '入住率',
			mc: {
				rs: 1,
				cs: 2,
			},
		},
		{
			m: '',
			prop: 'occupancyRate',
			setColor: true,
			archives: true,
			archivesText: '入住老人花名册',
			showLink: (col, model) => {
				drawerOldman.value = true
			},
		},
	],
	[
		{ m: '开户银行' },
		{
			setColor: true,
			m: '',
			prop: 'bankName',
			mc: {
				rs: 1,
				cs: 2,
			},
		},
		{
			m: '银行账号',
			mc: {
				rs: 1,
				cs: 2,
			},
		},
		{ setColor: true, m: '', prop: 'bankAccount' },
	],
]
const dataListStaff = [
	[
		{
			m: '员 工 概 况',
			bl: 1,
			fs: 16,
			style: { borderTop: '0px solid #fff' },
			mc: {
				rs: 1,
				cs: 8,
			},
			setColor: true,
			archives: true,
			archivesText: '医生、护士、护理员花名册',
			showLink: (col, model) => {
				const creditCode = modelData.value.creditCode
				setGloablParams({ creditCode: creditCode })
				drawer.value = true
			},
		},
	],
	[
		{ m: '管理人员' },
		{ m: '', prop: 'orgManagerNum' },
		{ m: '持证人数' },
		{ m: '', prop: 'certifiedPersonnelTotal' },
		{ m: '医技人数' },
		{ m: '', prop: 'medicalTechnicalStaffTotal' },
		{ m: '护士人数' },
		{ m: '', prop: 'nurseTotal' },
	],
	[
		{ m: '护理员数' },
		{ m: '', prop: 'caregiverTotal' },
		{ m: '持证人数' },
		{ m: '', prop: 'caregiverCertifiedTotal' },
		{ m: '工勤人数' },
		{ m: '', prop: 'utilityStaffTotal' },
		{ m: '员工总数' },
		{ m: '', prop: 'staffTotal' },
	],
]

const applyConfigBase = (row?: any) => {
	if (!row) row = {}
	return [
		[
			{
				m: '申请内容（床位核算）',
				bl: 1,
				fs: 16,
				style: { borderTop: '0px solid #fff' },
				mc: {
					rs: 1,
					cs: 8,
				},
			},
		],
		[
			{ m: '单人间数' },
			{ m: '', prop: 'singleRoomTotal' },
			{
				m: '双人间数',
			},
			{ m: '', prop: 'doubleRoomTotal' },
			{ m: '三人间数' },
			{ m: '', prop: 'tripleRoomTotal' },
			{ m: '多人间数' },
			{ m: '', prop: 'multiPersonRoomTotal' },
		],
		[
			{ m: '房间总数' },
			{ m: '', prop: 'roomTotal' },
			{ m: '床位总数' },
			{
				m: '',
				prop: 'bedTotal',
			},
			{
				m: '平均床位建筑面积',
				mc: {
					rs: 1,
					cs: 2,
				},
			},
			{
				m: '',
				prop: 'averageBedBuildingArea',
				mc: {
					rs: 1,
					cs: 2,
				},
			},
		],
		[
			{
				m: '产权性质（新建/租赁）',
				mc: {
					rs: 2,
					cs: 1,
				},
				style: {
					width: '170px',
				},
			},
			{
				m: '',
				prop: 'propertyTypeLabel',
				style: {
					width: '80px',
				},
				mc: {
					rs: 2,
					cs: 1,
				},
			},
			{ m: '申报普通型床位数' },
			{ m: '', prop: 'generalBedTotal' },
			{
				m: '补贴标准（元/ 张）',
				mc: {
					rs: 1,
					cs: 2,
				},
			},
			{
				m: '',
				prop: 'generalBedSubsidy',
				mc: {
					rs: 1,
					cs: 2,
				},
			},
		],
		[
			{ m: '申报护理型床位数' },
			{ m: '', prop: 'nursingBedTotal' },
			{
				m: '补贴标准（元/ 张）',
				mc: {
					rs: 1,
					cs: 2,
				},
			},
			{
				m: '',
				prop: 'nursingBedSubsidy',
				mc: {
					rs: 1,
					cs: 2,
				},
			},
		],
		[
			{
				m: '补贴金额',
				align: 'left',
				mc: {
					rs: 2,
				},
			},
			{
				m: '普通型：',
				align: 'left',
				propName: row.applySubsidyAmount,
				mc: {
					rs: 1,
					cs: 2,
				},
			},
			{
				m: '大写:',
				align: 'left',
				propName: row.applySubsidyAmountCap,
				mc: {
					rs: 1,
					cs: 2,
				},
			},
			{
				m: '总金额',
				align: 'left',
				propName: row.applySumAmount,
				mc: {
					rs: 2,
				},
			},
			{
				m: '大写',
				align: 'left',
				propName: row.applySumAmountCap,
				mc: {
					cs: 2,
					rs: 2,
				},
			},
		],
		[
			{
				m: '护理型：',
				align: 'left',
				propName: row.applyNurseSubsidyAmount,
				mc: {
					rs: 1,
					cs: 2,
				},
			},
			{
				m: '大写:',
				align: 'left',
				propName: row.applyNurseSubsidyAmountCap,
				mc: {
					cs: 2,
				},
			},
		],
		[
			{
				m: '本机构承诺以上及所附数据资料真实有效，如有不实，愿承担相关处罚。',
				prop: 'note',
				mc: {
					rs: 2,
					cs: 8,
				},
			},
		],
	]
}
const dataListApply: any = ref([...applyConfigBase()])

const dataListFooter = [
	[
		{
			m: '审核意见',
			bl: 1,
			fs: 16,
			style: { borderTop: '0px solid #fff' },
			mc: {
				rs: 1,
				cs: 9,
			},
		},
	],
  [
		{
			m: '街（镇）民政部门意见',
			mc: {
				rs: 1,
				cs: 1,
			},
		},
		{
			m: '',
			prop: 'opinionS',
			mc: {
				rs: 1,
				cs: 7,
			},
		},
	],
   [
		{
			m: '第三方现场核查意见',
			mc: {
				rs: 1,
				cs: 1,
			},
		},
		{
			m: '',
			prop: 'opinionT',
			mc: {
				rs: 1,
				cs: 7,
			},
		},
	],
	[
		{
			m: '区民政部门意见',
			mc: {
				rs: 1,
				cs: 1,
			},
		},
		{
			m: '',
			prop: 'opinion',
			mc: {
				rs: 1,
				cs: 7,
			},
		},
	],
]

const getInitData = async () => {
	const params = props.params || {}
	const query = {
		year: '',
		month: '',
		creditCode: props.creditCode,
		projectId: params.subsidyProjectId,
		applySource: !props.approvalStatus ? '-99' : props.approvalStatus,
    uniqueId: props.otherData?.id
	}
	if (params.belongDate) {
		query.year = params.belongDate.replace('年', '-').replace('月', '').split('-')[0]
		query.month = params.belongDate.replace('年', '-').replace('月', '').split('-')[1]
		model.value.year = query.year
		model.value.month = query.month
	} else {
		return
	}
	modelData.value = props.editData
	try {
		const condition = (props.caseId && props.isDetail) || props.custome || props.approvalStatus == -2
		loading.value = true
		let resultData = props.editData
    baseModel.value = resultData
		// if(props.approvalStatus ==undefined && !props.otherData?.allData) {
		if(!condition && !props.otherData?.allData) {
			const uptoData = await apiMod.view_orgBedSubsidyOne(query).then((res) => res.data)
			if (uptoData.occupancyRate) {
				uptoData.occupancyRate = uptoData.occupancyRate.toFixed(2)*100 + '%'
			}

			resultData = uptoData
			baseModel.value = JSON.parse(JSON.stringify(uptoData))
		}
		const reportData = filterEmptyObj(resultData)

		let mixData = {
			...props.editData,
			...reportData,
		}
		loading.value = false
		modelData.value = {
			...mixData,
		}
		if (baseModel.value.isEligible == 0) {
			ElMessageBox.alert(baseModel.value.reason || '当前申请条件不满足！', '提示', {
				showClose: false,
				confirmButtonText: '确定',
				callback: () => {
					pageReload()
				},
			})
			return false
		}
		if (!condition) handleSave()

		dataListApply.value = [...applyConfigBase(modelData.value)]
	} catch (error) {
		loading.value = false
	}
}

watch(
	() => props.editData.id,
	(val) => {
		if (val) {
			if (props.params) getInitData()
		}
	},
	{
		immediate: true,
	}
)
onMounted(() => {
	nextTick(() => {
		if (props.params) getInitData()
	})
})
const handleSave = () => {
	emit('save', modelData.value, 'bed_report_1005', props.editData)
}
onUnmounted(() => {
	// emit('save', modelData.value, 'report_1001',props.editData)
})
</script>

<style scoped lang="scss">
.p-r-50 {
	padding-right: 200px;
}
.text-underline {
	text-decoration: underline;
	padding: 0 6px;
}
.report-table-wrapper {
	width: 100%;
	margin: 0 auto;
	padding: 10px 30px;
	top: 0;

	.r-header {
		text-align: center;
		font-size: 18px;
		font-weight: bold;
		padding: 15px 0;

		.text-sm {
			font-weight: 400;
			font-size: 16px;
		}
	}
}
</style>
